//
//  BViewController.swift
//  自定义tabbar和navigationbar
//
//  Created by lete on 2020/4/24.
//  Copyright © 2020 lete. All rights reserved.
//

import UIKit

class BViewController: UIViewController {
    
    var message : NSString?
    let screenSize =  UIScreen.main.bounds.size
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = UIColor.gray
        self.navigationItem.title = "B"
        
        initNavBtn()
        
        rewriteBackBtn()
        
        
        
        
        
    }
    
    func rewriteBackBtn() {
        let leftBarBtn = UIBarButtonItem(title: "返回", style: .plain, target: self,
                                         action: #selector(backToPrevious))
        leftBarBtn.image = UIImage(named: "back")
        
        //用于消除左边空隙，要不然按钮顶不到最前面
        let spacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil,
                                     action: nil)
        spacer.width = -10
        
        self.navigationItem.leftBarButtonItems = [spacer, leftBarBtn]
    }
    
    
    
    //返回按钮点击响应
    @objc func backToPrevious(){
        print("返回")
        self.navigationController!.popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    
    //Mark: nav页面
    //初始化按钮，点击按钮跳转页面
    func initNavBtn(){
        
        let navBtn  = UIButton(type: UIButton.ButtonType.custom)
        navBtn.setTitle("nav跳转", for: .normal)
        navBtn.frame = CGRect(x: screenSize.width/2-50, y: screenSize.height/5-50, width: 100, height: 100)
        navBtn.backgroundColor = UIColor(red: 50/255, green: 123/255, blue: 203/255, alpha: 1)
        navBtn.setTitleColor(UIColor.white, for: .normal)
        //按钮绑定事件，点击时执行
        navBtn.addTarget(self, action:  #selector(pageNavJump), for: .touchDown)
        
        self.view.addSubview(navBtn)
        
    }
    @objc func pageNavJump() {
        //创建一个页面(纯代码)
        //  let nextVc = BlockViewController()
        
        ////创建一个页面(Storyboard)
        let sb = UIStoryboard(name: "MsgStoryboard", bundle: nil)
        let blockVC = sb.instantiateViewController(withIdentifier: "blockPage") as! BlockViewController
        
        //取目标页面的一个变量进行赋值，以属性的方式进行传值。
        blockVC.name = "nav传递的值"
        
        blockVC.blo = {str in
            print("========== \(str)")
        }
        //跳转
        self.navigationController?.pushViewController(blockVC, animated: true)
    }
    
}
